
*********************************************************************
* (1.0) Calculate Sex Ratio at Birth, combining 3rd and higher births
*********************************************************************
use 	"$f_2PKbirthpanel", clear	

drop if pregresult>2
gen pregyear = year 
replace pregmonth = pregmonth/12
gen pregtime = pregyear+pregmonth

sort id pregtime
by id: gen order=_n

keep prov id pregyear pregmonth birthorder mbirth order cmale child_death_age 

drop  pregmonth
drop if mbirth >1
drop mbirth
drop birthorder
reshape wide pregyear cmale child_death_age, i(prov id) j(order)

matrix sexratio_birth = J(34, 7, .) 

local i = 1
forval year = 1955/1988 { 

local allboys = 0
local allgirls = 0 

	cap qui sum cmale1 if pregyear1 == `year' 
	if _rc==0 {
		matrix sexratio_birth[`i',1] = `year'
		local boys = r(sum)
		local girls = r(N)-r(sum)
		matrix sexratio_birth[`i',2] = `boys'/`girls' 
		
		local allboys 	= `allboys' + `boys' 
		local allgirls 	= `allgirls' + `girls'
		}		
	cap qui sum cmale2 if pregyear2 == `year' & cmale1==1
	if _rc==0 {
		local boys = r(sum)
		local girls = r(N)-r(sum)
		matrix sexratio_birth[`i',3] = `boys'/`girls' 
		
		local allboys 	= `allboys' + `boys' 
		local allgirls 	= `allgirls' + `girls'
		}	
	cap qui sum cmale2 if pregyear2 == `year' & cmale1==0
	if _rc==0 {
		local boys = r(sum)
		local girls = r(N)-r(sum)
		matrix sexratio_birth[`i',4] = `boys'/`girls' 
		
		local allboys 	= `allboys' + `boys' 
		local allgirls 	= `allgirls' + `girls'
		}	
	cap qui sum cmale3 if pregyear3 == `year' & (cmale1==1 | cmale2==1) 
	if _rc==0 {
		local boys3 = r(sum)
		local girls3 = r(N)-r(sum)
		}
	cap qui sum cmale4 if pregyear4 == `year' & (cmale1==1 | cmale2==1 | cmale3 ==1) 
	if _rc==0 {
		local boys4 = r(sum)
		local girls4 = r(N)-r(sum)
		}	
	cap qui sum cmale5 if pregyear5 == `year' & (cmale1==1 | cmale2==1 | cmale3 ==1 | cmale4 == 1) 
	if _rc==0 {
		local boys5 = r(sum)
		local girls5 = r(N)-r(sum)
		}		
	cap qui sum cmale6 if pregyear6 == `year' & (cmale1==1 | cmale2==1 | cmale3 ==1 | cmale4 == 1 | cmale5 == 1) 
	if _rc==0 {
		local boys6 = r(sum)
		local girls6 = r(N)-r(sum)
		}

	local boys = `boys3' + `boys4' + `boys5' + `boys6'  
	local girls = `girls3' + `girls4' + `girls5' + `girls6' 
	matrix sexratio_birth[`i',5] = `boys'/`girls' 

		
		local allboys 	= `allboys' + `boys' 
		local allgirls 	= `allgirls' + `girls'
	
	cap qui sum cmale3 if pregyear3 == `year' & cmale1==0 & cmale2==0 
	if _rc==0 {
		local boys3 = r(sum)
		local girls3 = r(N)-r(sum)
		}		
	cap qui sum cmale4 if pregyear4 == `year' & cmale1==0 & cmale2==0 & cmale3==0 
	if _rc==0 {
		local boys4 = r(sum)
		local girls4 = r(N)-r(sum)
		}
	cap qui sum cmale5 if pregyear5 == `year' & cmale1==0 & cmale2==0 & cmale3==0 & cmale4==0
	if _rc==0 {
		local boys5 = r(sum)
		local girls5 = r(N)-r(sum)
		}
	cap qui sum cmale6 if pregyear6 == `year' & cmale1==0 & cmale2==0 & cmale3==0 & cmale4==0 & cmale5 == 0
	if _rc==0 {
		local boys6 = r(sum)
		local girls6 = r(N)-r(sum)
		}
	
	local boys = `boys3' + `boys4' + `boys5' + `boys6'
	local girls = `girls3' + `girls4' + `girls5' + `girls6' 
	matrix sexratio_birth[`i',6] = `boys'/`girls'
		
		local allboys 	= `allboys' + `boys' 
		local allgirls 	= `allgirls' + `girls'
			
	matrix sexratio_birth[`i',7] = `allboys'/`allgirls'

	local i = `++i'
}

matrix colnames sexratio_birth =  "year" "first_srb" 	"second_malesib_srb" "second_nomalesib_srb" ///
														"third_malesib_srb" "third_nomalesib_srb" ///
														 "overall_srb"
	
*********************************
* (2.0) Plot results
********************************* 
clear 
svmat sexratio_birth, names(col)
	drop if year <=1960 | year>1987

	replace first_srb = first_srb*100
	replace second_malesib_srb = second_malesib_srb*100
	replace second_nomalesib_srb = second_nomalesib_srb*100
	replace third_malesib_srb = third_malesib_srb*100
	replace third_nomalesib_srb = third_nomalesib_srb*100

	twoway 	(lpoly first_srb year, lcolor(black) lpattern(solid) lwidth(medthick) bw(.6)) ///
			(lpoly second_malesib_srb year, lcolor(black) lpattern(shortdash) lwidth(medthick) bw(.8)) ///
			(lpoly second_nomalesib_srb year, lcolor(black) lpattern(dash_dot) lwidth(medthick) bw(.8)) ///
			(lpoly third_malesib_srb year, lcolor(black) lpattern(longdash) lwidth(medthick) bw(.8)) ///
			(lpoly third_nomalesib_srb year, lcolor(black) lpattern(dash_3dot) lwidth(medthick) bw(.8))  ///
			(pcarrowi 165 1970 165 1979 , barbsize(1)) (pcarrowi 165 1979 165 1970, barbsize(1) ) ///
			if year>=1962, xtitle("Birth Year") xlab(1962(2)1988, angle(forty_five))  ytitle("Sex Ratio at Birth") ///
			text(165 1974.5 "LLF Policy" "Implementation" "Range", size(small) box bcolor(white) fcolor(white)) ///
			text(165 1984 "One Child" "Policy" "(Starting 1980)", box bcolor(white) fcolor(white) size(small)) yline(105.5, lcolor(black)) ///
			legend(order(1 "First Births" - "" 3 "Second Births; No Male Sibling" 2 "Second Births; Male Sibling"  5 "Third- and Higher-Order Births; No Male Sibling" 4 "Third- and Higher-Order Births; Male Sibling") ///
			pos(6) cols(2) size(small) span) title(" ") ysize(12) xsize(20) yscale(range(100 180)) ///
			ylab(100(10)180) xline(1970, lpattern(dash)) xline(1979, lpattern(dash))
	graph export "$d_fig\Fig_1b_SRB.jpg", replace 

